package com.weixing.mall.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.core.enums.OrderAppTagEnum;
import com.weixing.mall.core.enums.OrderSysTagEnum;
import com.weixing.mall.base.enums.WhetherEnum;
import com.weixing.mall.provider.mapper.OrderPayMapper;
import com.weixing.mall.provider.model.domain.Order;
import com.weixing.mall.provider.model.domain.OrderPay;
import com.weixing.mall.provider.model.enums.OrderAuditStateEnum;
import com.weixing.mall.provider.model.enums.OrderFrontStateEnum;
import com.weixing.mall.provider.model.enums.OrderStateEnum;
import com.weixing.mall.provider.service.IOrderAgentService;
import com.weixing.mall.provider.service.IOrderPayService;
import com.weixing.mall.provider.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 订单模块-订单支付表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-12
 */
@Service
public class OrderPayServiceImpl extends ServiceImpl<OrderPayMapper, OrderPay> implements IOrderPayService {
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IOrderAgentService orderAgentService;

    @Override
    public OrderPay findByOrderId(Long orderId) {
        return getOne(new QueryWrapper<OrderPay>().eq("order_id", orderId));
    }

    @Override
    public OrderPay findByOutTradeNo(String outTradeNo) {
        return getOne(new QueryWrapper<OrderPay>().eq("out_trade_no", outTradeNo));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void execQuerySuccess(Order order, OrderPay orderPay) {
        order.setIsPay(WhetherEnum.YES.getValue());
        order.setOrderStatus(OrderStateEnum.HAS_PAY.getValue());
        order.setFrontOrderStatus(OrderFrontStateEnum.WAIT_SHIP.getValue());
        order.setAuditStatus(OrderAuditStateEnum.AGENT_WAIT_AUDIT.getValue());
        order.setSysTag(OrderSysTagEnum.YFK_TAG_103.getValue());
        order.setAppTag(OrderAppTagEnum.YFK_TAG_101.getValue());
        orderService.updateById(order);
        updateById(orderPay);
        orderAgentService.updateEnable(order.getId());
    }

    /**
     * 方法描述: 支付回调
     * @Author Small
     * @param order
     * @param orderPay
     * @Date 2020/1/15 18:40
     * @return void
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void execNotifySuccess(Order order, OrderPay orderPay) {
        order.setIsPay(WhetherEnum.YES.getValue());
        order.setOrderStatus(OrderStateEnum.HAS_PAY.getValue());
        order.setFrontOrderStatus(OrderFrontStateEnum.WAIT_SHIP.getValue());
        order.setAuditStatus(OrderAuditStateEnum.AGENT_WAIT_AUDIT.getValue());
        order.setSysTag(OrderSysTagEnum.YFK_TAG_103.getValue());
        order.setAppTag(OrderAppTagEnum.YFK_TAG_101.getValue());
        // 判断是否绑定了代理商
        orderService.updateById(order);
        updateById(orderPay);
        orderAgentService.updateEnable(order.getId());
    }
}
